package com.huyn.baseframework.net;

import com.huyn.baseframework.utils.ApiUtils;
import com.huyn.baseframework.utils.Constant;
import com.huyn.baseframework.utils.UserUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author huyn
 * @version 2013-12-9 下午4:43:26
 */

public class ParamParser {

	private static HashMap<String, String> getParaMaps(HashMap<String, String> params) {
		if(!params.containsKey(Constant.APP_KEY_APPSOURCE)) {
			params.put(Constant.APP_KEY_APPSOURCE, Constant.APP_SOURCE);
		}
		if(!params.containsKey(Constant.APP_KEY_OSTYPE)) {
			params.put(Constant.APP_KEY_OSTYPE, Constant.OS_TYPE);
		}
		if(!params.containsKey(Constant.APP_KEY_OSVERSION)) {
			params.put(Constant.APP_KEY_OSVERSION, android.os.Build.VERSION.RELEASE);
		}
		//api version
		if(!params.containsKey(Constant.APP_KEY_APIVERSION)) {
			params.put(Constant.APP_KEY_APIVERSION, "1");
		}

		if(!params.containsKey(Constant.APP_KEY_VERSION)) {
			params.put(Constant.APP_KEY_VERSION, Constant.APP_VERSION);
		}

		/**应用补丁之后的真实版本*/
		if(!params.containsKey(Constant.APP_KEY_TRUE_VERSION)) {
			params.put(Constant.APP_KEY_TRUE_VERSION, Constant.APP_TRUE_VERSION);
		}

		if(!params.containsKey(Constant.APP_KEY_DEVICEID)) {
			params.put(Constant.APP_KEY_DEVICEID, Constant.APP_DEVICEID);
		}
		if(!params.containsKey(Constant.APP_KEY_MOBILETYPE)) {
			params.put(Constant.APP_KEY_MOBILETYPE, Constant.APP_MOBILETYPE);
		}
		if(!params.containsKey(Constant.APP_KEY_IMEI)) {
			params.put(Constant.APP_KEY_IMEI, Constant.APP_IMEI);
		}
		if(!params.containsKey(Constant.APP_KEY_MEMBERID)) {
			params.put(Constant.APP_KEY_MEMBERID, UserUtil.getMemberId());
		}
		if(!params.containsKey(Constant.APP_KEY_MEMBERTOKEN)) {
			params.put(Constant.APP_KEY_MEMBERTOKEN, UserUtil.getMemberEncode());
		}
		if(!params.containsKey(Constant.APP_KEY_APPKEY)) {
			params.put(Constant.APP_KEY_APPKEY, Constant.APP_KEY);
		}
		return params;
	}

	public static HashMap<String, String> ParserParamsAPI(HashMap<String, String> params) {
		params = getParaMaps(params);

		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String> mRemoveList = new ArrayList<String>();

		for (Map.Entry<String, String> item : params.entrySet()) { 
			if(item.getValue()==null){
				mRemoveList.add(item.getKey());
			}
			list.add(item.getKey());
		}
		
		int removeCount = mRemoveList.size();
		for(int i=0;i<removeCount;i++){
			params.remove(mRemoveList.get(i));
			list.remove(mRemoveList.get(i));
		} 
		 
		Collections.sort(list);
		int listSize = list.size();
		TreeMap<String, String> treeMap = new TreeMap<String, String>();
		for(int i=0;i<listSize;i++){
			treeMap.put(list.get(i), params.get(list.get(i)));
		}
		
		String sign = "";
		try{
			sign = ApiUtils.signRequest(treeMap, Constant.getPrivateKey());
		}catch (Exception e) {
		}

		params.put("sign", sign);
		params.put("signmethod", "MD5");
		 
		return params;
	}

}
